﻿using System;
using System.Data.SqlServerCe;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SenseTimeTracker.BusinessLayer;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace SenseTimeTracker.Tasks
{
	public partial class TaskEditForm : CommonSenseControls.CommonSenseForm
	{
		private Task task;
		private bool sipInitialized = false;

		private SensePanelComboItem clientFilterComboBox;
		private SensePanelComboItem projectFilterComboBox;
		private SensePanelTextboxItem taskNameItem;
		private SensePanelTextboxItem taskNumberItem;
		private SensePanelTextboxItem taskTypeItem;
		private SensePanelTextboxItem taskLocationItem;
		private SensePanelCheckboxItem taskCompletedItem;
		private SensePanelCheckboxItem lockCompletedItem;
		private SensePanelNumericItem taskEstimateItem;
		private SensePanelTextboxItem taskDescriptionItem;
		private SensePanelTextboxItem taskDefaultDescriptionItem;
		private SensePanelCheckboxItem taskExportTerpLocationItem;
		private SensePanelComboItem taskCategoryItem;
		private SensePanelButtonItem taskColorItem;

		public TaskEditForm()
		{
			InitializeComponent();

			senseHeaderCtrl.Text = Resources.TaskEditHeader;

			clientFilterComboBox = new SensePanelComboItem();
			clientFilterComboBox.LabelText = string.Format("{0}:", Resources.ClientLabel);
			clientFilterComboBox.ShowSeparator = true;
			clientFilterComboBox.OnSelectedValueChanged += clientFilterComboBox_OnSelectedValueChanged;

			projectFilterComboBox = new SensePanelComboItem();
			projectFilterComboBox.LabelText = string.Format("{0}:", Resources.ProjectLabel);
			projectFilterComboBox.ShowSeparator = true;

			taskNameItem = new SensePanelTextboxItem();
			taskNameItem.LabelText = string.Format("{0}:", Resources.NameLabel);
			taskNameItem.MaxLength = DatabaseManager.MaxTaskNameLength;
			taskNameItem.ShowSeparator = true;

			taskNumberItem = new SensePanelTextboxItem();
			taskNumberItem.LabelText = string.Format("{0}:", Resources.NumberLabel);
			taskNumberItem.MaxLength = DatabaseManager.MaxTaskNumberLength;
			taskNumberItem.ShowSeparator = true;

			taskTypeItem = new SensePanelTextboxItem();
			taskTypeItem.LabelText = string.Format("{0}:", Resources.TypeLabel);
			taskTypeItem.MaxLength = DatabaseManager.MaxTaskTypeLength;
			taskTypeItem.ShowSeparator = true;

			taskCategoryItem = new SensePanelComboItem();
			taskCategoryItem.LabelText = string.Format("{0}:", Resources.CategoryLabel);
			taskCategoryItem.ShowSeparator = true;
			foreach (Category taskCategory in Category.Categories)
			{
				taskCategoryItem.Items.Add(new SensePanelComboItem.Item(taskCategory.Name, taskCategory.Id));
			}

			taskColorItem = new SensePanelButtonItem();
			taskColorItem.Text = Resources.ColorLabel;
			taskColorItem.ShowSeparator = true;

			taskLocationItem = new SensePanelTextboxItem();
			taskLocationItem.LabelText = string.Format("{0}:", Resources.LocationLabel);
			taskLocationItem.MaxLength = DatabaseManager.MaxTaskLocationLength;
			taskLocationItem.ShowSeparator = true;

			taskCompletedItem = new SensePanelCheckboxItem();
			taskCompletedItem.PrimaryText = string.Format("{0}:", Resources.CompletedLabel);
			taskCompletedItem.ShowSeparator = true;

			lockCompletedItem = new SensePanelCheckboxItem();
			lockCompletedItem.PrimaryText = string.Format("{0}:", Resources.LockCompletedLabel);
			lockCompletedItem.ShowSeparator = true;

			taskEstimateItem = new SensePanelNumericItem();
			taskEstimateItem.PrimaryText = string.Format("{0}:", Resources.EstimationLabel);
			taskEstimateItem.Value = 0;
			taskEstimateItem.Minimum = 0;
			taskEstimateItem.Maximum = 1000;
			taskEstimateItem.ButtonAnimation = true;
			taskEstimateItem.OnClick += OnTaskEstimationClick;
			taskEstimateItem.ShowSeparator = true;

			taskDefaultDescriptionItem = new SensePanelTextboxItem();
			taskDefaultDescriptionItem.LabelText = string.Format("{0}:", Resources.DefaultDescriptionLabel);
			taskDefaultDescriptionItem.Text = string.Empty;
			taskDefaultDescriptionItem.MaxLength = DatabaseManager.MaxTaskDefaultDescriptionLength;
			taskDefaultDescriptionItem.ShowSeparator = true;

			taskDescriptionItem = new SensePanelTextboxItem();
			taskDescriptionItem.LabelText = string.Format("{0}:", Resources.DescriptionLabel);
			taskDescriptionItem.Multiline = true;
			taskDescriptionItem.Height = 200;
			taskDescriptionItem.MaxLength = DatabaseManager.MaxTaskDescriptionLength;
			taskDescriptionItem.Text = string.Empty;
			taskDescriptionItem.ShowSeparator = true;

			taskExportTerpLocationItem = new SensePanelCheckboxItem();
			taskExportTerpLocationItem.PrimaryText = string.Format("{0}:", Resources.TerpLocationLabel);
			taskExportTerpLocationItem.Status = ItemStatus.Off;
			taskExportTerpLocationItem.ShowSeparator = true;
		}

		public Task Task
		{
			get { return task; }
			set { task = value; }
		}

		private void SetupListItems()
		{
			senseListCtrl.Clear();
			senseListCtrl.AddItem(new SensePanelDividerItem(string.Empty, Resources.TaskEditExplantion));
			senseListCtrl.AddItem(clientFilterComboBox);
			senseListCtrl.AddItem(projectFilterComboBox);
			senseListCtrl.AddItem(taskNameItem);
			senseListCtrl.AddItem(taskNumberItem);
			senseListCtrl.AddItem(taskTypeItem);
			senseListCtrl.AddItem(taskCategoryItem);
			senseListCtrl.AddItem(taskColorItem);
			senseListCtrl.AddItem(taskLocationItem);
			senseListCtrl.AddItem(taskCompletedItem);
			senseListCtrl.AddItem(lockCompletedItem);
			senseListCtrl.AddItem(taskEstimateItem);
			senseListCtrl.AddItem(taskDefaultDescriptionItem);
			senseListCtrl.AddItem(taskDescriptionItem);
			senseListCtrl.AddItem(taskExportTerpLocationItem);

			if (!sipInitialized)
			{
				sipInitialized = true;
				SIP.Enable(senseListCtrl.Handle);
			}
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			Binder.BindCombobox(clientFilterComboBox.Items, Guid.Empty, Guid.Empty, true, false);
			clientFilterComboBox.SelectedValue = task.Client;

			Binder.BindCombobox(projectFilterComboBox.Items, task.Client, Guid.Empty, true, false);
			projectFilterComboBox.SelectedValue = task.Project;

			taskNameItem.Text = task.Name;
			taskNumberItem.Text = task.Number;
			taskTypeItem.Text = task.Type;
			taskLocationItem.Text = task.Location;
			taskCompletedItem.Status = task.Completed ? ItemStatus.On : ItemStatus.Off;
			lockCompletedItem.Status = task.LockCompleted ? ItemStatus.On : ItemStatus.Off;
			taskCategoryItem.SelectedValue = task.Category;
			taskEstimateItem.Value = task.EstimatedTime;
			taskDescriptionItem.Text = task.Description;
			taskDefaultDescriptionItem.Text = task.DefaultDescription;
			taskExportTerpLocationItem.Status = task.TerpExportLocation ? ItemStatus.On : ItemStatus.Off;

			SetupListItems();
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			if (DialogResult == DialogResult.OK)
			{
				task.Client = (Guid)clientFilterComboBox.SelectedValue;
				task.Project = (Guid)projectFilterComboBox.SelectedValue;
				task.Name = taskNameItem.Text;
				task.Number = taskNumberItem.Text;
				task.Type = taskTypeItem.Text;
				task.Location = taskLocationItem.Text;
				task.Completed = taskCompletedItem.Status == ItemStatus.On;
				task.LockCompleted = lockCompletedItem.Status == ItemStatus.On;
				task.Category = (Guid)taskCategoryItem.SelectedValue;
				task.Description = taskDescriptionItem.Text;
				task.DefaultDescription = taskDefaultDescriptionItem.Text;
				task.EstimatedTime = taskEstimateItem.Value;
				task.TerpExportLocation = taskExportTerpLocationItem.Status == ItemStatus.On;


				// check for propper selected client
				if (task.Client == DatabaseManager.AllClientsId)
				{
					if (MessageBox.Show(Resources.DialogInvalidSelectedClient, Resources.DialogHeaderInvalidInput, MessageBoxButtons.RetryCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Retry)
					{
						e.Cancel = true;
					}
					else
					{
						DialogResult = DialogResult.Cancel;
					}

					return;
				}

				// check for propper selected project
				if (task.Project == DatabaseManager.AllProjectsId)
				{
					if (MessageBox.Show(Resources.DialogInvalidSelectedProject, Resources.DialogHeaderInvalidInput, MessageBoxButtons.RetryCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Retry)
					{
						e.Cancel = true;
					}
					else
					{
						DialogResult = DialogResult.Cancel;
					}

					return;
				}

				// check for propper task name
				if (string.IsNullOrEmpty(task.Name))
				{
					if (SenseAPIs.SenseMessageBox.Show(Resources.DialogInvlidEmptyName, Resources.DialogHeaderInvalidInput, SenseMessageBoxButtons.RetryCancel) == DialogResult.Retry)
					{
						e.Cancel = true;
					}
					else
					{
						DialogResult = DialogResult.Cancel;
					}

					return;
				}
			}

			base.OnClosing(e);
		}

		void OnTaskEstimationClick(object Sender)
		{
			int v = (Sender as SensePanelNumericItem).Value;
			if (SenseAPIs.SenseNumericBox.Show(Resources.TaskEstimationHeader, ref v) == DialogResult.OK)
			{
				(Sender as SensePanelNumericItem).Value = v;
			}
		}

		/// <summary>
		/// Event on client index changed
		/// </summary>
		/// <param name="sender">Sender of the event</param>
		/// <param name="e">Event argumentns</param>
		void clientFilterComboBox_OnSelectedValueChanged(object Sender, object Value)
		{
			projectFilterComboBox.Items.Clear();
			Binder.BindCombobox(projectFilterComboBox.Items, (Guid)Value, Guid.Empty, true, false);
			projectFilterComboBox.SelectedValue = DatabaseManager.AllProjectsId;
			SetupListItems();
		}
	}
}